package com.sifa.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

@Slf4j
@RestController
public class FlowLimitController {


    @GetMapping("testA")
    public String testA(){
      return "-----testA";
    }
    @GetMapping("testB")
    public String testB(){

     return  "----------testB";
    }

    @GetMapping("testD")
    public String testD() throws InterruptedException {

        Thread.sleep(2000);
        log.info("testD");
        return  "---------TestD";
    }

    @GetMapping("testE")
    public String testE(){
        log.info("执行TestE");
        Random random=new Random();
        int i = random.nextInt();
        log.info(String.valueOf(i));
        if (i%2==0){
            log.info("这不报错吗");
            System.out.println(10/0);
        }
        return "正常执行";
    }
    @GetMapping("testHotKey")
    @SentinelResource(value = "testHotKey",blockHandler = "hand_testHotKey",fallback = "handleFallback")
    public String testHotKey(@RequestParam(value = "p1",required = false)String p1,
                             @RequestParam(value = "p2",required = false)String p2){
        System.out.println(10/0);

     return "testHotKey";
    }

    public String hand_testHotKey(String p1, String p2, BlockException blockException){

        return "DEL_HOTKEY";
    }
    public String handleFallback(){
        return "异常了";
    }


}
